// tnxcore-fss.js
/**
 * 文件存储服务的客户端支持<br>
 * 文件存储服务的部署方式有三种：
 * - 独立应用：作为独立部署的微服务向其它应用提供服务，具有标准的RPC客户端
 * - 子应用：嵌入在某个应用中，作为虚拟的应用向其它应用提供服务，具有引用型的RPC客户端，其内部ref属性指向所属应用的RPC客户端
 * - 非应用：嵌入在某个应用中，且不向其它应用提供服务，适用于系统只包含一个应用的简单场景，没有专属的RPC客户端
 */
const fss = {
    PROTOCOL: 'fss://',
    appName: undefined,
    getClientConfig(appName) {
        const rpc = window.tnx.app.rpc;
        // 默认配置为非应用部署时的配置
        let config = {
            appName: this.appName,
            baseUrl: rpc.getDefaultBaseUrl(),
            contextUrl: '/fss',
        }
        appName = appName || this.appName;
        if (appName) {
            let client = rpc.appClients[appName];
            if (client) {
                config.appName = appName;
                if (client.ref) { // 子应用
                    config.baseUrl = client.ref.baseUrl;
                } else { // 独立应用
                    config.baseUrl = client.baseUrl;
                }
                config.contextUrl = '';
            }
        }
        return config;
    },
    loadUploadOptions(type, params, options) {
        return new Promise((resolve, reject) => {
            let config = this.getClientConfig();
            options = options || {};
            if (!options.app) {
                options.app = config.appName;
            }
            window.tnx.app.rpc.get(config.contextUrl + '/upload-options/' + type, params, options)
                .then(resolve)
                .catch(reject);
        });
    },
    getUploadUrl(type, scope, withBaseUrl) {
        let config = this.getClientConfig();
        let url = config.contextUrl + '/upload/' + type;
        if (withBaseUrl) {
            url = config.baseUrl + url;
        }
        if (scope !== undefined && scope !== null) {
            url += '/' + scope;
        }
        return url;
    },
    upload(type, scope, file, options) {
        return new Promise((resolve, reject) => {
            let formData = new FormData();
            formData.append('file', file);
            let url = this.getUploadUrl(type, scope);
            options = options || {};
            options.headers = {'Content-Type': 'multipart/form-data'};
            options.app = options.app || this.getClientConfig().appName;
            window.tnx.app.rpc.post(url, formData, options).then(resolve).catch(reject);
        });
    },
    resolveDownloadUrl(locationUrl, options = {}) {
        return new Promise((resolve, reject) => {
            let config = this.getClientConfig();
            if (!options.app) {
                options.app = config.appName;
            }
            window.tnx.app.rpc.get(config.contextUrl + '/download/url', {locationUrl}, options)
                .then(resolve)
                .catch(reject);
        });
    },
    readText(locationUrl, options = {}) {
        return new Promise((resolve, reject) => {
            let config = this.getClientConfig();
            if (!options.app) {
                options.app = config.appName;
            }
            window.tnx.app.rpc.get(config.contextUrl + '/read/text', {locationUrl}, options)
                .then(resolve)
                .catch(reject);
        });
    }
};

export default fss;
